
clear
set more off
cap log close

************************************************************************
** set macros (directory)
************************************************************************
global prison_dir "~/Dropbox/Prison_Covid/PNAS_Nexus_Replication"
global raw_data "${prison_dir}/raw_data"
global intermediate_data "${prison_dir}/intermediate_data"
global code "${prison_dir}/code"
global analysis_data "${prison_dir}/analysis_data"
global output "${prison_dir}/output"

************************************************************************
** Produce Figure 1
************************************************************************

use "${intermediate_data}/CA_prison_location_matchedbg.dta", clear 
rename gidbg w_bg
merge m:1 w_bg using "${intermediate_data}/LODES_tot_jobs_JT01_2020_byprisonBG.dta", nogenerate
gen si03_per_prison_ = wbg_si03 / num_prison_in_bg
gen s000_per_prison_ = wbg_s000 / num_prison_in_bg

// sum it up if a prison has two BGs - which is true for one prison: CORRECTIONAL TRAINING FACILITY (CTF)
gegen si03_per_prison = total(si03_per_prison_) if si03_per_prison_!=. , by(NAME)
gegen s000_per_prison = total(s000_per_prison_) if s000_per_prison_!=. , by(NAME)
drop s000_per_prison_ si03_per_prison_

// collapse to prison level
sort NAME 
drop if NAME == NAME[_n-1]

// get smartphone counts
merge 1:1 NAME using "${intermediate_data}/prison_phones_count_June.dta"
	
reg si03_per_prison phones_count
// scatter plot
tw (scatter si03_per_prison phones_count if num_prison_in_bg == 1 & si03_per_prison != ., xtitle("Smartphones Count") ytitle("Imputed Employment Level (LODES)") ) ///
	(scatter si03_per_prison phones_count if num_prison_in_bg == 2 & si03_per_prison != ., mcolor(orange) xlabel(0(100)500) ylabel(0(1000)4000) ) /// 
	(lfit si03_per_prison phones_count if si03_per_prison != .,  lpattern(dot) lcolor(dkgreen) text(3300 450 " R{superscript:2} = 0.367") ///
	legend(order(1 "Block group has one prison" 2 "Block group has two prisons" 3 "Linear Fit")  ))	
graph export "${output}/figure1.png", replace 
